/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aexbanner;

import fontys.observer.RemotePropertyListener;
import java.beans.PropertyChangeEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Administrator
 */
public class BannerController extends UnicastRemoteObject implements RemotePropertyListener {

    private IEffectenbeurs beurs;
    private Timer getTimer;
    private AexBanner banner;
    // Set binding name for student administration
    private static String bindingName = "RemoteEffectenbeurs";
    // References to registry and student administration
    private Registry registry = null;

    public BannerController(String ipAddress, int portNumber) throws Exception {

        // Print IP address and port number for registry
        System.out.println("Client: IP Address: " + ipAddress);
        System.out.println("Client: Port number " + portNumber);

        // Bind student administration

        // Locate registry at IP address and port number
        registry = locateRegistry(ipAddress, portNumber);

        // Print result locating registry
        if (registry != null) {
            System.out.println("Client: Registry located");
        } else {
            System.out.println("Client: Cannot locate registry");
            System.out.println("Client: Registry is null pointer");
        }


        if (registry != null) {
            beurs = bindEffectenbeursUsingRegistry();
        }


        if (beurs != null) {
            System.out.println("Client: Efectenbeurs bound");
            try {

                beurs.addListener(this, "fondsen");
                banner = new AexBanner();
                // getTimer = new Timer();
                //  getTimer.schedule(new BannerTimerTask(beurs, banner),1000,1000);
                banner.setVisible(true);
            } catch (Exception e) {
                throw e;
            }
        } else {
            System.out.println("Client: Effectenbeurs is null pointer");
        }



    }

    public static void main(String[] args) throws Exception {
        System.setProperty("java.security.policy",
                "rmi.policy");

        System.setSecurityManager(new RMISecurityManager());


        Scanner input = new Scanner(System.in);
        System.out.print("Client: Enter IP address of server: ");
        String ipAddress = input.nextLine();

        // Get port number
        System.out.print("Client: Enter port number: ");
        int portNumber = input.nextInt();

        BannerController controller = new BannerController(ipAddress, portNumber);
    }

    // Locate registry
    private Registry locateRegistry(String ipAddress, int portNumber) {

        // Locate registry at IP address and port number
        Registry registry = null;
        try {
            registry = LocateRegistry.getRegistry(ipAddress, portNumber);
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot locate registry");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            registry = null;
        }
        return registry;
    }

    // Bind student administration using registry
    private IEffectenbeurs bindEffectenbeursUsingRegistry() {

        // Bind student administration
        IEffectenbeurs beurs = null;
        try {
            Object o = registry.lookup(bindingName);
            System.out.println(o.getClass().toString());
            beurs = (IEffectenbeurs) o;
        } catch (RemoteException ex) {
            System.out.println("Client: Cannot bind Effectenbeurs");
            System.out.println("Client: RemoteException: " + ex.getMessage());
            beurs = null;
        } catch (NotBoundException ex) {
            System.out.println("Client: Cannot bind student administration");
            System.out.println("Client: NotBoundException: " + ex.getMessage());
            beurs = null;
        }
        return beurs;
    }

    @Override
    public void propertyChange(PropertyChangeEvent pce) throws RemoteException {

        IFonds[] fondsen = (IFonds[]) pce.getNewValue();
        String s = "";

        for (IFonds f : fondsen) {
            s += f.getNaam() + " : " + f.getKoers() + " ";
        }

        banner.setKoersen(s);

    }

    class BannerTimerTask extends TimerTask {

        private IEffectenbeurs beurs;
        private AexBanner banner;

        public BannerTimerTask(IEffectenbeurs beurs, AexBanner banner) {
            this.beurs = beurs;
            this.banner = banner;
        }

        @Override
        public void run() {

            try {
                IFonds[] fondsen = beurs.getKoersen();
                String s = "";

                for (IFonds f : fondsen) {
                    s += f.getNaam() + " : " + f.getKoers() + " ";
                }

                banner.setKoersen(fondsen);
            } catch (Exception e) {
            }
        }
    }

    class MinimalPolicy extends Policy {

        private PermissionCollection perms;

        public MinimalPolicy() {
            super();
            if (perms == null) {
                perms = new MyPermissionCollection();
                addPermissions();
            }
        }

        @Override
        public PermissionCollection getPermissions(CodeSource codesource) {
            return perms;
        }

        private void addPermissions() {
          
        }
        
         class MyPermissionCollection extends PermissionCollection
        {

            @Override
            public void add(Permission permission) {
                
            }

            @Override
            public boolean implies(Permission permission) {
                return true;
            }

            @Override
            public Enumeration<Permission> elements() {
                ArrayList<Permission> per = new ArrayList<Permission>();
                per.add(new AllPermission());
                return(Enumeration<Permission>) per;
            }
            
        }
    }
}
